﻿YUI.add('schedulerCalendarizacion', function (Y) {
    'use strict';
    Y.SchedulerCalendarizacion = Y.Base.create('SchedulerCalendarizacion', Y.View, [], {
        templateIsCharged: false,
        loadTemplate: function () {
            if (this.template == '') {
                var that = this,
                validator = null,
                request = Y.io('/ExcellentProject/scripts/views/examenes/calendarizaciones/templates/scheduler.html', {
                    on: {
                        complete: function (id, response) {
                            that.templateIsCharged = true;
                            that.template = response.responseText;
                            that.render();
                        }
                    },
                    failure: function (id, response) {
                        alert(response)
                    }
                })
            }
        },
        render: function () {
            debugger
            this.loadTemplate();
            if (this.templateIsCharged) {
                this.get('container').setHTML(this.template);
                debugger
                var model = this.get('model');

                var event =
                      {
                          content: model.get('Descripcion'),
                          endDate: new Date(model.get('FechaCierre')),
                          startDate: new Date(model.get('Fecha')),
                          color: '#A4A4A4'
                      };


                var fechascalendarizaciones = new Y.Calendarizaciones();
                var that = this, a = fechascalendarizaciones.load({ urlName: 'rangocalendarizaciones', fecha: model.get('Fecha').substring(0, 10) }, function () {
                    var lista = [];
                    var data = fechascalendarizaciones.toArray()
                    var aux = null;
                    for (var i = 1; i < data.length; i++) {
                        aux = data[i].getAttrs();
                        lista.push({
                            content: aux['Descripcion'],
                            endDate: new Date(aux['FechaCierre']),
                            startDate: new Date(aux['Fecha'])
                        });
                    }
                    debugger
                    lista.push(event)
                    var agendaView = new Y.SchedulerAgendaView();
                    var dayView = new Y.SchedulerDayView();
                    var eventRecorder = new Y.SchedulerEventRecorder();
                    var monthView = new Y.SchedulerMonthView();
                    var weekView = new Y.SchedulerWeekView();
                    new Y.Scheduler(
                      {
                          boundingBox: '#myScheduler',
                          date: new Date(),
                          items: lista,
                          render: true,
                          //views: [weekView]
                          views: [dayView, weekView, monthView, agendaView]
                      }
                    );
                });


            }
        },
    })
}, '1.0.0.0', { requires: ['handlebars', 'handlebars-select-helper', 'node', 'aui-scheduler', 'calendarizacion-model-list', 'calendarizacion-model'] })